package com.scaffold.photo.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.photo.entity.Photo;
import com.scaffold.photo.service.PhotoService;

/**
 * 照片操作接口(json)
 * @author wangjg
 */
@Controller
public class PhotoJsonAction extends BasicBizAction{
	@Autowired
	private PhotoService photoService;

	/**
	 * 照片列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/photo/list.json")
	public String list(Long albumId, String title, String path, String remark, String keyword){
		Page page = this.getPage(20);
		List<Photo> list = photoService.pageQuery(page, albumId, title, path, remark, keyword);
		return this.getPageResult(page, list);
	}

	/**
	 * 获取照片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/photo/get.json")
	public String get(Long id){
		Photo photo = photoService.getPhoto(id);
		this.checkOwner(photo);
		return this.getSuccessResult(photo);
	}

	/**
	 * 保存照片
	 */
	@RequestMapping("/photo/save.json")
	public String save(Long id){
		if(id==null){
			Photo photo = new Photo();
			this.bindParams(photo);
			this.setOwner(photo);
			this.photoService.save(photo);
		}else{
			Photo photo = this.photoService.getPhoto(id);
			this.checkOwner(photo);
			this.bindParams(photo);
			this.photoService.update(photo);
		}
		return this.getSuccessResult();
	}

	/**
	 * 启用/禁用照片
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/photo/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		Photo photo = photoService.getPhoto(id);
		this.checkOwner(photo);
		this.photoService.update(photo);
		return this.getSuccessResult(enabled);
	}

	/**
	 * 删除照片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/photo/delete.json")
	public String delete(Long id){
		Photo photo = photoService.getPhoto(id);
		this.checkOwner(photo);
		this.photoService.delete(photo);
		return this.getSuccessResult();
	}

	/**
	 * 批量删除照片
	 * @param idseq 编号序列(以‘,’号隔开)
	 * @return
	 */
	@RequestMapping("/photo/batchDelete.json")
	public String batchDelete(String idseq){
		this.photoService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	/**
	 * 获取照片列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/photo/select.json")
	public String select(Long albumId, String title, String path, String remark, String keyword){
		List<Photo> list = photoService.pageQuery(null, albumId, title, path, remark, keyword);
		return this.getSuccessResult(list);
	}


}
